package com.waterworld.watch.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.bean.AddressBookBean;
import com.waterworld.watch.common.customview.CircleImageView;
import com.waterworld.watch.common.net.UrlContants;
import com.waterworld.watch.common.util.PhotoUtils;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.home.event.AddressBookEvent;
import com.waterworld.watch.home.event.ContactsAvatarEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SkillContactInfoActivity extends BaseActivity  implements View.OnClickListener{


    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.header_confirm)
    TextView header_confirm;

    @BindView(R.id.iv_contact_avatar)
    CircleImageView iv_contact_avatar;

    @BindView(R.id.et_contact_name)
    EditText et_contact_name;

    @BindView(R.id.et_contact_phone)
    EditText et_contact_phone;

    @BindView(R.id.cb_normal_call)
    CheckBox cb_normal_call;

    @BindView(R.id.cb_urgent_call)
    CheckBox cb_urgent_call;

    @BindView(R.id.cb_single_listener)
    CheckBox cb_single_listener;

    @BindView(R.id.btn_delete)
    Button btn_delete;

    private String imgName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skill_contact_info);
        ButterKnife.bind(this);
        initView();
        EventBus.getDefault().register(this);
    }


    private void initView(){
        setViewSize(header_parent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this) / 10);
        header_back.setVisibility(View.VISIBLE);
        header_title.setVisibility(View.VISIBLE);
        header_confirm.setVisibility(View.VISIBLE);

        header_back.setOnClickListener(this);
        header_confirm.setOnClickListener(this);
        iv_contact_avatar.setOnClickListener(this);
        btn_delete.setOnClickListener(this);

        if(getIntent().getSerializableExtra("contacts") != null) {
            //修改联系人
            header_title.setText(getString(R.string.edit_contacts));
            btn_delete.setVisibility(View.VISIBLE);
            AddressBookBean addressBookBean = (AddressBookBean) getIntent().getSerializableExtra("contacts");
            imgName = addressBookBean.getHead();
            if (addressBookBean.getHead() == null){
                //默认头像
                Glide.with(this).load(R.drawable.headimage_default).into(iv_contact_avatar);
            } else if (addressBookBean.getHead().startsWith("sys")){
                Glide.with(this).load(PhotoUtils.getResource(addressBookBean.getHead().substring(0,13))).into(iv_contact_avatar);
            } else {
                Glide.with(this).load(UrlContants.FILE_URL + "resources/watch/" + addressBookBean.getHead()).into(iv_contact_avatar);
            }
            et_contact_name.setText(addressBookBean.getName());
            et_contact_phone.setText(addressBookBean.getPhone());
            if (addressBookBean.getRole() != null) {
                for (String item : addressBookBean.getRole().split(",")) {
                    if ("1".equals(item)) {
                        cb_normal_call.setChecked(true);
                    } else if ("2".equals(item)) {
                        cb_urgent_call.setChecked(true);
                    } else if ("3".equals(item)) {
                        cb_single_listener.setChecked(true);
                    }
                }
            }
        } else {
            //新增联系人
            header_title.setText(R.string.add_contacts);

        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.header_back:
                finish();
                break;
            case R.id.header_confirm:
                if (TextUtils.isEmpty(et_contact_name.getText().toString())) {
                    ToastUtils.showShort(this,getString(R.string.plz_input_nickname));
                } else if (TextUtils.isEmpty(et_contact_phone.getText().toString())) {
                    ToastUtils.showShort(this,getString(R.string.plz_input_nickname));
                } else {
                    AddressBookBean addressBookBean = new AddressBookBean();
                    addressBookBean.setHead(imgName);
                    addressBookBean.setName(et_contact_name.getText().toString());
                    addressBookBean.setPhone(et_contact_phone.getText().toString());
                    StringBuilder sb = new StringBuilder();
                    if (cb_normal_call.isChecked()) {
                        sb.append("1,");
                    }
                    if (cb_urgent_call.isChecked()) {
                        sb.append("2,");
                    }
                    if (cb_single_listener.isChecked()) {
                        sb.append("3");
                    }
                    String role = sb.toString();
                    if (role.endsWith(",")) {
                        role = role.substring(0, role.length() - 1);
                    }
                    addressBookBean.setRole(role);
                    AddressBookEvent event = new AddressBookEvent();
                    event.setAddressBookBean(addressBookBean);
                    event.setPosition(getIntent().getIntExtra("position", 0));
                    EventBus.getDefault().post(event);
                    finish();
                }
                break;
            case R.id.iv_contact_avatar:
                startActivity(new Intent(this,ContactsAvatarActivity.class));
                break;
            case R.id.btn_delete:
                AddressBookEvent event = new AddressBookEvent();
                event.setDelete(true);
                event.setPosition(getIntent().getIntExtra("position", 0));
                EventBus.getDefault().post(event);
                finish();
                break;
            default:
                break;
        }
    }

    //EventBus设置头像数据
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setContactsAvatar(ContactsAvatarEvent avatar){
        if (avatar.getType() == 0) {
            //自定义头像,在拍照完后上传至服务器，再从服务器获取图片uri,便于统一操作
            //图片路径baseUrl/resources/watch/+head
            imgName = avatar.getName();
            Glide.with(this).load(UrlContants.FILE_URL +"resources/watch/"+imgName).into(iv_contact_avatar);
        } else if (avatar.getType() == 1) {
            //系统头像
            imgName = avatar.getName();
            Glide.with(this).load(PhotoUtils.getResource(imgName.substring(0,13))).into(iv_contact_avatar);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
